/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>

#include "fsl_common.h"
#include "board.h"

#include "i2c_hal.h"


/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Prototypes
 ******************************************************************************/
void cmd_i2c_slave_init(void);
volatile uint32_t cmd_i2c_slave_counter;

volatile uint32_t cmd_i2c_slave_counter_pre;

/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */
int main(void)
{
    board_init();
    printf("i2c slave basic.\r\n");

    cmd_i2c_slave_init();

    while (1)
    {
        //getchar();
        if (cmd_i2c_slave_counter_pre != cmd_i2c_slave_counter)
        {
            printf("cmd_i2c_slave_counter: %d\r\n", cmd_i2c_slave_counter);
            cmd_i2c_slave_counter_pre = cmd_i2c_slave_counter;
        }
    }
}

#define CMD_I2C_SLAVE_ADDRESS 0x45 /* 7-bit address. 100_1100. */
#define CMD_I2C   I2C0

#define CMD_I2C_SLAVE_XFER_BUFF_LEN  (200u+1u)
uint8_t cmd_i2c_slave_xfer_buff[CMD_I2C_SLAVE_XFER_BUFF_LEN]; /* the func code is at the first byte. */

void cmd_i2c_slave_stop_callback(uint32_t flags)
{
    cmd_i2c_slave_counter++;
}


void cmd_i2c_slave_init(void)
{
    cmd_i2c_slave_counter = 0u;
    cmd_i2c_slave_counter_pre = 0u;

    i2c_conf_slave(CMD_I2C, CMD_I2C_SLAVE_ADDRESS, cmd_i2c_slave_xfer_buff, CMD_I2C_SLAVE_XFER_BUFF_LEN);
    i2c_slave_install_stop_callback(CMD_I2C, cmd_i2c_slave_stop_callback);
}





/* EOF. */

